package org.hry.socket.test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Desc
 * @Author houry
 * @Date 2021/2/25 9:11
 **/
public class NioClientSocket {

    public static List<String> cache = new ArrayList<>();
    public static List<Integer> b = new ArrayList<>();


    public static void main(String[] args) throws IOException {
        readFIle();
        for (int j = 0; j < 50; j++) {
            Random random = new Random();
            int i = random.nextInt(cache.size());
            if (!b.contains(i)) {
                hhhh(cache.get(i));
                b.add(i);
            }
        }
    }

    public static void readFIle() {
        String pathname = "C:\\Users\\houry\\Desktop\\title.txt";
        try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader)) {
            String line;
            while ((line = br.readLine()) != null) {
                if (line.startsWith("#")) {
                    cache.add(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void hhhh(String msg) {
        String pathname = "C:\\Users\\houry\\Desktop\\title.txt";
        try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader)) {
            String line;
            while ((line = br.readLine()) != null) {
                if (msg.equals(line)) {
                    writeFile("## " + msg.substring(1));
                    System.out.println(msg);
                    while (true) {
                        String s = br.readLine();
                        if (!s.startsWith("#")) {
                            writeFile(s);
                        } else {
                            break;
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile(String msg) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\houry\\Desktop\\a.txt", true));
        bw.write(msg);
        bw.newLine();
        bw.flush();
    }

}

